<?php
/**
 * Created by PhpStorm.
 * User:admin
 * Date: 14-8-30
 * Time: 下午8:14
 */
class RegisterController extends Controller{

    private $_time;

    public function init(){
        parent::init();
        $this->_time = time();
    }

    public function indexAction(){
        define('ITEM_PER_PAGE', 10);
        $this->_title = '注册';
        $this->_keywords = '注册';
        $this->_description = '注册';
        $this->_view->title = $this->_title;
        $this->_view->keywords  = $this->_keywords;
        $this->_view->description = $this->_description;
    }

//收入手机号和验证码
    public function steptowAction(){
        $Post = $this->_request->getPost();
        if(empty($Post['mobile'])){
            $this->redirect ( '/register/index' );
            die();
        }
        if(empty($Post['codetime'])){
            $this->redirect ( '/register/index' );
            die();
        }
        $codetime = $this->_session->offsetGet('codetime');
        if($codetime != $Post['codetime']){
            $this->redirect ( '/register/index' );
            die();
        }

        $this->_view->mobile = $Post['mobile'];
        $this->_view->codetime = $Post['codetime'];
    }

    public function successAction(){

    }

    public function registerAction(){
        $Post = $this->_request->getPost();

        $this->_view->title =  "用户注册";
        $this->_view->login_name =$Post['login_name'];
        $this->_view->password =$Post['password'];
        $this->_view->password_confirm =$Post['password_confirm'];
    }
//保存用户信息
    public function saveregAction(){
        if($this->_request->isPost()) {
            global $_SmsSendSwitch,$_SmsSendContent;
            $Post = $this->_request->getPost();
            $Post['mobile'] = empty($Post['mobile']) ? null : htmlspecialchars(trim($Post['mobile']), ENT_QUOTES, 'UTF-8');
            $Post['passwd'] = empty($Post['passwd']) ? null : htmlspecialchars(trim($Post['passwd']), ENT_QUOTES, 'UTF-8');
            $Post['repasswd'] = empty($Post['repasswd']) ? null : htmlspecialchars(trim($Post['repasswd']), ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? null : htmlspecialchars(trim($Post['codetime']), ENT_QUOTES, 'UTF-8');
            $codetime = $this->_session->offsetGet('codetime');

            if (empty($Post['mobile'])) {
                $retdata = array(
                    'code' => '33',
                    'msg' => '手机号码为空'
                );
                echo json_encode($retdata);
                die();
            }
            if ($Post['passwd'] != $Post['repasswd']) {
                $retdata = array(
                    'code' => '34',
                    'msg' => '两次密码输入不正确'
                );
                echo json_encode($retdata);
                die();
            }

            if (!Tools::checkPassword($Post['passwd'])) {
                $retdata = array(
                    'code' => '36',
                    'msg' => '密码输入不规则'
                );
                echo json_encode($retdata);
                die();
            }


            if ($Post['codetime'] == $codetime) {

                //判断是否重复
                $user = new UserModel();
                $isExistUser = $user->isExistByRow('moblie', $Post['mobile']);
                if (!empty($isExistUser)) {
                    $retdata = array(
                        'code' => '37',
                        'msg' => '该用户已经存在'
                    );
                    echo json_encode($retdata);
                    die();
                }


                //开始插入数据
                $logion_key = "su_";
                $logion_key .= Tools::getRandNums(6);//随机生成６位数字

                $data = array(
                    'user_source' => 'M',
                    'login_item' => 'M',
                    'user_type' => 'B',
                    'user_level' => '1',
                    'password' => md5($Post['passwd']),
                    'login_name' => $logion_key,
                    'moblie' => $Post['mobile'],
                    'reg_date' => date('Y-m-d H:i:s', $this->_time)
                );

                $ret = $user->add($data);

                if(!empty($ret)){

                    //注册成功开始进行发送短信
                    if($_SmsSendSwitch['op_reg'] ==1){
                        $sms = new sendSmsJxt($this->_db_handler,null);
                        $content = str_replace('%d1',$Post['mobile'],$_SmsSendContent['op_reg_c']);
                        $sms->otherSend($Post['mobile'],$content);
                    }

                    //开始存入登录信息
                    if(!$this->_session->has('loginInfos')) {
                        $this->_session->offsetSet('loginInfos',array());
                    }
                    $loginInfo = array();
                    $loginInfo['id']      = $ret;
                    $loginInfo['login_name']   = $logion_key;
                    $loginInfo['moblie']  = $Post['mobile'];
                    $this->_session->offsetSet('loginInfos',$loginInfo);

                    $retdata = array(
                        'code' => '38',
                        'msg' => '用户注册成功！'
                    );
                    echo json_encode($retdata);
                    unset($_SESSION['codetime']);
                    die();
                }

            } else {
                $retdata = array(
                    'code' => '35',
                    'msg' => '不合法的访问！'
                );
                echo json_encode($retdata);
                die();
            }
        }
    }

    //是否手机存在
    public function isExistMobileAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $user = new UserModel();
            $isExistUser = $user->isExistByRow('moblie', $Post['mobile']);
            if (!empty($isExistUser)) {
                $retdata = array(
                    'code' => '37',
                    'msg' => '该用户已经存在'
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '38',
                    'msg' => '该用户不经存在'
                );
                echo json_encode($retdata);
                die();
            }
        }
        die();
    }

    public function confirmcodeAction(){

        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['code'] = empty($Post['code']) ? '' : htmlspecialchars($Post['code'], ENT_QUOTES, 'UTF-8');
            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');

            $code = $this->_session['captcha'];

            if(strtoupper($code) == strtoupper($Post['code'])){
                $codetime = time();
                $this->_session->offsetSet('codetime',$codetime);
                $retdata = array(
                    'code' => '8',
                    'msg'  => '验证码输入正确',
                    'data' => array(
                        'vmobile' =>  $Post['mobile'],
                        'vcode' => $codetime
                    )
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '9',
                    'msg'  => '验证码输入不正确',
                );
                echo json_encode($retdata);
                die();
            }

        }
    }
//判断手机验证码是否正确
    public function confirmPhonecodeAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['code'] = empty($Post['code']) ? '' : htmlspecialchars($Post['code'], ENT_QUOTES, 'UTF-8');
            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');

            $send = new SendsmsModel();
            $where="receiver ='{$Post['mobile']}' and code='{$Post['code']}' and send_time < '{$this->_time}' and (send_time+1800) > '{$this->_time}' and type = 'Register' AND status=1";
            $field=array(
                'code'
            );
            $ret = $send->getRowByWhere($where,$field);
            if(empty($ret)){
                $retdata = array(
                    'code' => '99',
                    'msg'  => '验证码输入不正确',
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '88',
                    'msg'  => '验证码输入正确',
                );
                echo json_encode($retdata);
                die();
            }
        }
        die();
    }
//发送验证码
    public function sendcodeAction(){
        global $_MessageCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();

            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? '' : htmlspecialchars($Post['codetime'], ENT_QUOTES, 'UTF-8');

            if(empty($Post['mobile'])){
                $ret_data="-5";
                echo json_encode($ret_data);
                die();
            }
            $codetime = $this->_session->offsetGet('codetime');

            $code = Tools::getRandNums(6);
            if($Post['codetime'] == $codetime){
                //开始发送短信
                $data = array(
                    'code' => $code,
                    'mobile' => $Post['mobile'],
                    'content' => "您正在注册会员操作，动态验证码为:".$code."（30分钟有效）如非本人操作请忽略",
                );
                $dataType = "Register";
                if($_MessageCfg['register']=='open'){
                    $ret = Sms::DatabaseSend($Post['mobile'],$data,1,$dataType);
                }
                if(!$ret){
                    $ret_data="-6";
                    echo json_encode($ret_data);
                    die();
                }
                echo json_encode($ret);
                die();
            }
            die();
        }
        die();
    }
}